﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure;

namespace OnlineCatalog.Controllers
{
    public class ProController : Controller
    {
        private Model1Container db = new Model1Container();

        //
        // GET: /Pro/

        public ActionResult Index()
        {
            var productset = db.ProductSet.Include(p => p.Category);
            return View(productset.ToList());
        }

        //
        // GET: /Pro/Details/5

        public ActionResult Details(int id = 0)
        {
            Product product = db.ProductSet.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        //
        // GET: /Pro/Create

        public ActionResult Create()
        {
            ViewBag.CategoryId = new SelectList(db.CategorySet, "Id", "Name");
            return View();
        }

        //
        // POST: /Pro/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                db.ProductSet.Add(product);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CategoryId = new SelectList(db.CategorySet, "Id", "Name", product.CategoryId);
            return View(product);
        }

        //
        // GET: /Pro/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Product product = db.ProductSet.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            ViewBag.CategoryId = new SelectList(db.CategorySet, "Id", "Name", product.CategoryId);
            return View(product);
        }

        //
        // POST: /Pro/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Product product, HttpPostedFileBase ImageFile)
        {
            if (ModelState.IsValid)
            {
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();

                var storageAccount = CloudStorageAccount.Parse(
                    CloudConfigurationManager.GetSetting("MyCloudStorage"));
                // BLOB specific
                var blobClient = storageAccount.CreateCloudBlobClient();

                var container = blobClient.GetContainerReference("demo");
                var fname = ImageFile.FileName;
                var blob = container.GetBlockBlobReference(fname);
                using (var fileSteam = ImageFile.InputStream)
                {
                    blob.Properties.ContentType = ImageFile.ContentType;
                    blob.Metadata.Add("ProductId", product.Id.ToString());
                    blob.UploadFromStream(fileSteam);
                }
                product.ImageUrl = blob.Uri.AbsoluteUri;

                db.SaveChanges();

                return RedirectToAction("Index");
            }
            ViewBag.CategoryId = new SelectList(db.CategorySet, "Id", "Name", product.CategoryId);
            return View(product);
        }

        //
        // GET: /Pro/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Product product = db.ProductSet.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        //
        // POST: /Pro/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Product product = db.ProductSet.Find(id);
            db.ProductSet.Remove(product);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}